const express = require('express');

const bodyParser = require('body-parser');

const app = express();
const noteModel = require('./db/model/noteModel');

app.use(bodyParser.urlencoded({extended:false}));

app.set('view engine', 'ejs');

app.set('views','./views');

app.get('/', (request, response)=>{
    noteModel.find((err,data)=>{
        if(err){
            response.send('查询数据库错误');
            return;
        }
        response.render('index',{data});
    })
})

app.post('/add', (request, response)=>{
    console.log(request.body);
    noteModel.create(request.body,(err,data)=>{
        response.redirect('/');
    })
})
app.get('/delete/:id',(request, response)=>{
    let id = request.params.id;
    noteModel.deleteOne({_id:id},(err,data)=>{
        if(err){
            response.send('删除失败');
            return;
        }
        response.redirect('/');
    })
})

app.listen(80,()=>{
    console.log('server run at 80');
})